import { App } from 'vue'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import {routerPersion} from "@/utils/permission"

const files = import.meta.globEager('./modules/*.ts')
let modules: Array<RouteRecordRaw> = []
Object.keys(files).forEach((path) => {
  modules = modules.concat((files[path] as any).default)
})

export const constantRoutes: Array<RouteRecordRaw> = [...modules]

const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
})

export function resetRouter() {
  ;(router as any).matcher = (router as any).matcher // reset router
}


export function setUpRouter(app: App) {
  app.use(router)
  routerPersion(router)
}

export default router
